<template>
  <div style="margin-top: -15px">
    <div style="margin-bottom: 15px">
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleAdd"
        >新增</el-button
      >
    </div>
    <el-table v-loading="loading" :data="sizeList" :height="tableHeight">
      <el-table-column label="序号" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="别名" align="center" prop="alias" />
      <el-table-column label="长边(毫米)" align="center" prop="longSide" />
      <el-table-column label="短边(毫米）" align="center" prop="shortSide" />
      <el-table-column label="高(毫米）" align="center" prop="height" />
      <el-table-column label="显示顺序" align="center" prop="sort" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改产品尺寸对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="名称" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入名称"
            :maxlength="20"
          />
        </el-form-item>
        <el-form-item label="别名" prop="alias">
          <el-input
            v-model="form.alias"
            placeholder="请输入别名"
            :maxlength="20"
          />
        </el-form-item>
        <el-form-item label="长边(毫米)" prop="longSide">
          <el-input
            v-model="form.longSide"
            placeholder="请输入长边(毫米）"
            type="number"
            oninput="this.value = this.value.replace(/[^0-9]/g, ''); 
           if(this.value > 99999) this.value = 99999;"
          />
        </el-form-item>
        <el-form-item label="短边(毫米）" prop="shortSide">
          <el-input
            v-model="form.shortSide"
            placeholder="请输入短边(毫米）"
            type="number"
            oninput="this.value = this.value.replace(/[^0-9]/g, ''); 
           if(this.value > 99999) this.value = 99999;"
          />
        </el-form-item>
        <el-form-item label="高(毫米）" prop="height">
          <el-input
            v-model="form.height"
            placeholder="请输入高(毫米）"
            type="number"
            oninput="this.value = this.value.replace(/[^0-9]/g, ''); 
           if(this.value > 99999) this.value = 99999;"
          />
        </el-form-item>
        <el-form-item label="显示顺序" prop="sort">
          <el-input
            v-model="form.sort"
            placeholder="请输入高显示顺序"
            type="number"
            oninput="this.value = this.value.replace(/[^0-9]/g, ''); 
           if(this.value > 99999) this.value = 99999;"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSize,
  getSize,
  delSize,
  addSize,
  updateSize,
} from "@/api/product/productSize.js";

export default {
  name: "Size",
  props: ["productId"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品尺寸表格数据
      sizeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //表格高度
      tableHeight: 400,
      // 查询参数
      queryParams: {
        productId: null,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        longSide: [
          { required: true, message: "长边不能为空", trigger: "blur" },
        ],
        shortSide: [
          { required: true, message: "短边不能为空", trigger: "blur" },
        ],
        sort: [{ required: true, message: "请输入正整数", trigger: "blur" }],
      },
    };
  },
  mounted() {
    this.$nextTick(() => {
      // 根据窗口高度计算表格高度
      this.tableHeight = window.innerHeight - 300; // 200是其他元素占用的高度
    });

    // 窗口大小变化时重新计算
    window.addEventListener("resize", this.calculateHeight);

    this.queryParams.productId = this.productId;
    this.getList();
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.calculateHeight);
  },
  methods: {
    // 表格高度
    calculateHeight() {
      this.tableHeight = window.innerHeight - 300;
    },
    //  查询产品尺寸列表
    getList() {
      this.loading = true;
      this.queryParams.productId = this.productId;
      listSize(this.queryParams).then((response) => {
        this.sizeList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        productId: null,
        name: undefined,
        alias: undefined,
        longSide: undefined,
        shortSide: undefined,
        height: undefined,
        sort: 0,
      };
      this.resetForm("form");
    },
    // 新增按钮操作
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加基本尺寸";
    },
    // 修改按钮操作
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSize(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改基本尺寸";
      });
    },
    // 提交按钮
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.productId = this.productId;
          if (this.form.id != null) {
            updateSize(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSize(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 删除按钮操作
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除产品尺寸名称为"' + row.name + '"的数据项？')
        .then(function () {
          return delSize(row.id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
